/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.web;

import cz.muni.fi.pa165.creatures.dto.web.PersonDTO;
import cz.muni.fi.pa165.creatures.services.PersonService;
import java.util.logging.Logger;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationError;

/**
 * Action bean for logging action.
 *
 * @author smikloso
 */
public class LoginActionBean extends CreatureHuntingActionBean {

    private static final Logger logger =
            Logger.getLogger(LoginActionBean.class.getName());

    @SpringBean
    PersonService personService;

    @Validate(required = true)
    private String username;

    @Validate(required = true)
    private String password;

    private String targetUrl;

    public String getUsername() {
        return username;
    }

    public void setUsername(String name) {
        this.username = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * The URL the user was trying to access (null if the login page was
     * accessed directly).
     */
    public String getTargetUrl() {
        return targetUrl;
    }

    /**
     * The URL the user was trying to access (null if the login page was
     * accessed directly).
     */
    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    /**
     * Logs in a user with password.
     * @return
     */
    public Resolution login() {
        if (this.username == null) {
            return new RedirectResolution("/register.jsp");
        }
        PersonDTO person = personService.getByName(this.username);
        if (person == null) {
            ValidationError error = new LocalizableError("user_not_exists");
            getContext().getValidationErrors().add("username", error);
            return getContext().getSourcePageResolution();
        } else if (!person.getPassword().equals(password)) {
            ValidationError error = new LocalizableError("password_incorect");
            getContext().getValidationErrors().add("password", error);
            return getContext().getSourcePageResolution();
        } else {
            getContext().setUser(person);
            if (this.targetUrl != null) {
                return new RedirectResolution(this.targetUrl);
            } else {
                return new RedirectResolution("/index.jsp");
            }
        }
    }
}
